package org.testmyapp;

import java.util.Date;
import java.util.concurrent.TimeUnit;

public class DateUtil {

	private DateUtil() {

	}

	public static Date addDays(int numberOfDays, Date base) {
		return new Date(base.getTime()
				+ (numberOfDays * TimeUnit.DAYS.toMillis(1)));
	}

	public static Date subDays(int numberOfDays, Date base) {
		return new Date(base.getTime()
				- (numberOfDays * TimeUnit.DAYS.toMillis(1)));
	}

	public static Integer differenceInDays(Date left, Date right) {
		int i = 0;
		while (left.before(right) || left.equals(right)) {
			i++;
			left = DateUtil.addDays(1, left);
		}
		return i;
	}

	public static boolean timeIntervalInOtherInterval(Date toBeInBegin,
			Date toBeInLast, Date isItInBegin, Date isItInEnd) {

		if ((toBeInBegin.before(isItInBegin) || toBeInBegin.equals(isItInBegin))
				&& (toBeInLast.after(isItInEnd) || toBeInLast.equals(isItInEnd))) {
			return true;
		}

		return false;
	}

	public static String dateToStringNoTime(Date date, String sep) {
		if (sep == null || sep.equals("")) {
			sep = ".";
		}
		String result = "";
		result += date.getDate();
		result += sep;
		result += (date.getMonth() + 1);
		result += sep;
		result += (date.getYear() + 1900);
		return result;
	}

}
